﻿using HCApp.UI.Windows.Views;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Linq;
using Syncfusion.Windows.Tools.Controls;
using System;
using System.Windows;
using LinnerToolkit.Core.Navigation;
using System.Windows.Threading;
using HCApp.UI.Windows.Dialogs;
using Syncfusion.Windows.Controls.Input;

namespace MainAppEntryPrismWpf.Views
{
    /// <summary>
    /// Interaction logic for RuKuManagePage
    /// </summary>
    public partial class RuKuManagePage : ContentPageBase
    {

        DispatcherTimer getComDataTimer;


        public DataClassesSCDBDataContext SCDBDataContent;

        private int MaxBH = -1;
        private int TodayNo = -1;

        private DSZX_CPCRKB SelectedYG222 = new DSZX_CPCRKB();

        private List<DSZX_CPCRKB> ygxxList;    //所有列表  同数据库中数据同
        private List<DSZX_CPCRKB> ygSignedList;

        DSZX_CLGZB SomeItem = null;


        DateTime certainDate;

        DateTime certainDateWhiteStart;
        DateTime certainDateNightEnd;


        public RuKuManagePage()
        {
            InitializeComponent();


            ygSignedList = new List<DSZX_CPCRKB>();
            ygxxList = new List<DSZX_CPCRKB>();

            SCDBDataContent = new DataClassesSCDBDataContext();

            datagrid.ItemsSource = (from data in SCDBDataContent.DSZX_CPCRKB
                                     where data.CJH== "单丝" && data.CKBZ==0
                                    select data).ToList();
              var lastRec= SCDBDataContent.DSZX_CPCRKB.ToList().LastOrDefault();
            if (lastRec!=null)
            {
                if (lastRec.CPBH.Substring(0,8) == DateTime.Now.ToString("yyyyMMdd"))
                {
                    TodayNo = int.Parse(lastRec.CPBH.Substring(8, 3));
                }
                else
                {
                    TodayNo = 0;
                }
            }
        
            ggSelect.ItemsSource = (from data in SCDBDataContent.DSZX_CPGGB where data.CJH=="单丝" select data.CPGG).ToList();

            ggYG.ItemsSource= (from data in SCDBDataContent.DSZX_YGXXB 
                               where data.CJH== "单丝"
                               select data.YGXM).ToList();
          

        }

    

        private void delete_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // ConfirmAlertDialog alertDialog = new ConfirmAlertDialog();
            if (MessageBox.Show("你确认要关机吗？", "警告", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
            {
                MessageBox.Show("确认关机");
            }
            else
            {
                MessageBox.Show("取消关机");

            }


        }
        private void add_Click(object sender, System.Windows.RoutedEventArgs e)
        {


            // if (this.zlTB.Text.ToString()==""|| this.zlTB.Text.Trim().ToString() == "0")

            string xx = this.zlTB.Value.ToString();
            if (this.zlTB.Value.ToString() == "0" || this.zlTB.Value <0.11)
            {
                MessageBox.Show("\n\n  请填写正确的重量！ \n\n",
                     "确认 ？");
                return;                
            }


            else if ( this.psTB.Text.ToString() == "" || this.psTB.Text.Trim().ToString() == "0")
            {
                MessageBox.Show("\n\n输入正确的盘数！ \n\n",
                     "确认 ？");
                return;
            }

            if (MessageBox.Show(" 请您核对这些待入库的数据.\n\n确定入库?",
                     "确认入库 ？",
                     MessageBoxButton.YesNo,
                     MessageBoxImage.Question) == MessageBoxResult.No)
            //   if (!WindowDialog.ShowDialog("请您核对这些待入库的数据.\n\n确定入库?").IsYes)
            {
                // Do something here
                return;
            }
            else {           
           


            certainDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd"));
            certainDateWhiteStart = certainDate.AddHours(7);
            certainDateNightEnd = certainDateWhiteStart.AddHours(24);

            int TotalPS = int.Parse(this.psTB.Text);
           for (int i = 0; i < TotalPS; i++)
            {
                var SelectedYG = new DSZX_CPCRKB();

                SelectedYG.CJH = "单丝";
                SelectedYG.CPGG = (this.ggSelect.SelectedItem as string);
                //   SelectedYG.CZR = this.czrTB.Text.ToString();
                SelectedYG.CZR = (this.ggYG.SelectedItem as string); 

                SelectedYG.RKSJ = DateTime.Now;
                // SelectedYG.PS= TotalPS;
                SelectedYG.PS = 1;
                    //   SelectedYG.CPDZ = double.Parse(this.zlTB.Value);
                    SelectedYG.CPDZ = this.zlTB.Value;

                    //  SelectedYG.CPZL = double.Parse(this.zlTB.Text) / ((int)int.Parse(this.psTB.Text));
                    // SelectedYG.CPZL = double.Parse(this.zlTB.Text)*TotalPS ;  //单个重量 单个序号
                   // SelectedYG.CPZL = double.Parse(this.zlTB.Text) ;  //单个重量 单个序号
                    SelectedYG.CPZL = this.zlTB.Value;;  //单个重量 单个序号
                    SelectedYG.CKBZ = 0;  //未出库
                TodayNo += 1;
                SelectedYG.CPBH= DateTime.Now.ToString("yyyyMMdd") + (TodayNo).ToString("000");
                SelectedYG.HSGZ =Math.Round( (double)SelectedYG.CPZL * GetZiXianDanjiaByGGB(SelectedYG.CPGG),3);
               SCDBDataContent.DSZX_CPCRKB.InsertOnSubmit(SelectedYG);
                SCDBDataContent.SubmitChanges();

                //查询   
                List<DSZX_CLGZB> foundSomeOne = (from data in SCDBDataContent.DSZX_CLGZB
                                                              where data.CJH == "单丝" && data.SCRQ >= certainDate && data.SCRQ < certainDateNightEnd &&data.YGXM== SelectedYG.CZR
                                                              select data).ToList();
                //找到需要的了
                if (foundSomeOne != null && foundSomeOne.Count > 0)
                {
                    //SomeItem= foundSomeOne[0];

                    foundSomeOne[0].SCCL =Math.Round( (double)(foundSomeOne[0].SCCL+ SelectedYG.CPZL),3);
                    foundSomeOne[0].YGGZ=Math.Round((double)(foundSomeOne[0].YGGZ+ SelectedYG.HSGZ),3);
                    SCDBDataContent.SubmitChanges();
                }
                //没找到就插入
                else
                {
                    DSZX_CLGZB ToAddRecord = new DSZX_CLGZB();
                    ToAddRecord.CJH = "单丝";
                    ToAddRecord.SCRQ = DateTime.Now;
                    ToAddRecord.CPGG = SelectedYG.CPGG;
                    ToAddRecord.YGBH = GetBHByXM(SelectedYG.CZR);
                    ToAddRecord.YGXM = SelectedYG.CZR;
                    ToAddRecord.SCCL =Math.Round( (double)SelectedYG.CPZL,3);
                    ToAddRecord.YGGZ =Math.Round( (double)SelectedYG.HSGZ,3);
                    SCDBDataContent.DSZX_CLGZB.InsertOnSubmit(ToAddRecord);
                    SCDBDataContent.SubmitChanges();
                    // ToAddRecord.YGBH = item.YGBH;
                }
            }         
         

            ygxxList = (from data
                       in SCDBDataContent.DSZX_CPCRKB
                        where data.CKBZ== 0 && data.CJH == "单丝"
                        select data).ToList();
            datagrid.ItemsSource = ygxxList;

            //    MessageBox.Show("已成功入库");
            }

        }

        private void datagrid_SelectionChanged(object sender, Syncfusion.UI.Xaml.Grid.GridSelectionChangedEventArgs e)
        {

        }


       

            private int  GetBHByXM(string ygXM)
        {
            var lastRec = SCDBDataContent.DSZX_YGXXB.Where(a => a.YGXM == ygXM).Select(b=>b.YGBH).FirstOrDefault();

            if (lastRec != null)
            {
                return (int)lastRec;
            }

            return 0;
        }
        private double GetZiXianDanjiaByGGB(string cpGG)
        {        
            var lastRec = SCDBDataContent.DSZX_CPGGB.Where( a => a.CPGG == cpGG).FirstOrDefault();

            if (lastRec != null)
            {
                return (double)lastRec.CPGJ;
            }

            return 0;
        }

        private void ContentPageBase_Loaded(object sender, RoutedEventArgs e)
        {
            
          //  getComDataTimer.Start();

        }

      
        protected override void InternalOnNavigatedFrom(AppNavigationParameters parameters)
        {
            base.InternalOnNavigatedFrom(parameters);
            try
            {
               
            }
            catch (Exception ee)
            {

               // throw;
            }
           
           
           
        }
    }
}
